package cn.jhc.bean;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.http.Cookie;

public class UserCookie {
	public static Cookie tranversal(Cookie[] cookies, String name)
	{
		
		for (Cookie c : cookies) {
			//判断有没有cookie
			if(c.getName().equals(name))
			{
				return c;
			}
		}
		
		return null;
	}
	
	public static List<CartItem> getCartItems(Cookie cookie)
	{
		List<CartItem> list = new ArrayList<>();
		String books = cookie.getValue();
//		System.out.println("books:" + books);
		StringTokenizer st1 = new StringTokenizer(books, "*");
		while (st1.hasMoreTokens() && !books.equals("")) 
		{
			String str = st1.nextToken();
//			System.out.println(str);
			StringTokenizer st2 = new StringTokenizer(str, "^");
			while (st2.hasMoreTokens() && !str.equals(""))
			{
				String isbn = st2.nextToken();				
				String num = st2.nextToken();
				
				CartItem cartItem =	new CartItem(isbn, Integer.parseInt(num));
				list.add(cartItem);
			}					
		}
		
		return list;
	}
	
}
